package com.sxfq.loansupermarket.backsystem.modular.report.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.report.dao.WholeHospitalsDao;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.SevenReportInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.WholeHospitalsInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.service.IGetTotlesService;
import com.sxfq.loansupermarket.backsystem.modular.report.service.ISevenReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SevenReportServiceImpl implements ISevenReportService {

    @Autowired
    private WholeHospitalsDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    @Autowired
    private IGetTotlesService getTotleService;

    @Override
    public List<WholeHospitalsInfo> selectPageListByQueryGetMap(Page<WholeHospitalsInfo> page, SevenReportInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getStartTime());
        }
        params.put("tableName", this.getTableName());
        return dao.getQueryPage(page, params);
    }


    @Override
    public List<WholeHospitalsInfo> getCustomerAgeList(SevenReportInfo searchInfo) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        String tableName1 = tableHead + "deal_info";
        String sql1 = " select a.id,a.age_range from " + tableName + " a left join " + tableName1 + " b on a.id=b.customer_id left join \n" +
                " pay_service_info c on b.pay_service_id=c.id   where a.arrival_time>='" + searchInfo.getStartTime() + "' and a.arrival_time<='" + searchInfo.getEndTime() + "' and c." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "' group by age_range order by a.age_range";
        List<SevenReportInfo> list1 = sqlMapper.selectList(sql1, SevenReportInfo.class);
        String sql = "";
        String classifications = "";
        if ("age_range".equals(searchInfo.getDimensionSelection()) || "one_medium".equals(searchInfo.getDimensionSelection())
                || "field_tc".equals(searchInfo.getDimensionSelection()) || "one_channel".equals(searchInfo.getDimensionSelection())
                || "cities".equals(searchInfo.getDimensionSelection()) || "occupation".equals(searchInfo.getDimensionSelection())) {
            classifications = " and a." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        } else {
            classifications = " and c." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        }
        for (int i = 0; i < list1.size(); i++) {
            String classification = list1.get(i).getAgeRange();
            String condition = "";
            if (classification == "" || classification == null) {
                condition = "a.age_range is null " + classifications;
            } else {
                condition = "a.age_range='" + classification + "'" + classifications;
            }
            sql += getTotleService.getsql(searchInfo, classification, condition, i, list1);
        }
        sql += getTotleService.getTotleSql(searchInfo, classifications);
        List<WholeHospitalsInfo> list = sqlMapper.selectList(sql, WholeHospitalsInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalsInfo> getTcNameList(SevenReportInfo searchInfo) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        String sql1 = " select a.id,a.field_tc from " + tableName + " a where a.arrival_time>='" + searchInfo.getStartTime() + "' and a.arrival_time<='" + searchInfo.getEndTime() + "' group by field_tc order by a.field_tc";
        List<SevenReportInfo> list1 = sqlMapper.selectList(sql1, SevenReportInfo.class);
        String sql = "";
        String classifications = "";
        if ("age_range".equals(searchInfo.getDimensionSelection()) || "one_medium".equals(searchInfo.getDimensionSelection())
                || "field_tc".equals(searchInfo.getDimensionSelection()) || "one_channel".equals(searchInfo.getDimensionSelection())
                || "cities".equals(searchInfo.getDimensionSelection()) || "occupation".equals(searchInfo.getDimensionSelection())) {
            classifications = " and a." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        } else {
            classifications = " and c." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        }
        for (int i = 0; i < list1.size(); i++) {
            String classification = list1.get(i).getFieldTc();
            String condition = "";
            if (classification == "" || classification == null) {
                condition = "a.field_tc is null" + classifications;
            } else {
                condition = "a.field_tc='" + classification + "'" + classifications;
            }
            sql += getTotleService.getsql(searchInfo, classification, condition, i, list1);
        }
        sql += getTotleService.getTotleSql(searchInfo, classifications);
        List<WholeHospitalsInfo> list = sqlMapper.selectList(sql, WholeHospitalsInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalsInfo> getOneMediumList(SevenReportInfo searchInfo) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        String sql1 = " select a.id,a.one_medium from " + tableName + " a where a.arrival_time>='" + searchInfo.getStartTime() + "' and a.arrival_time<='" + searchInfo.getEndTime() + "' group by one_medium order by a.one_medium";
        List<SevenReportInfo> list1 = sqlMapper.selectList(sql1, SevenReportInfo.class);
        String sql = " ";
        String classifications = "";
        if ("age_range".equals(searchInfo.getDimensionSelection()) || "one_medium".equals(searchInfo.getDimensionSelection())
                || "field_tc".equals(searchInfo.getDimensionSelection()) || "one_channel".equals(searchInfo.getDimensionSelection())
                || "cities".equals(searchInfo.getDimensionSelection()) || "occupation".equals(searchInfo.getDimensionSelection())) {
            classifications = " and a." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        } else {
            classifications = " and c." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        }
        for (int i = 0; i < list1.size(); i++) {
            String classification = list1.get(i).getOneMedium();
            String condition = "";
            if (classification == "" || classification == null) {
                condition = "a.one_medium is null" + classifications;
            } else {
                condition = "a.one_medium='" + classification + "'" + classifications;
            }
            sql += getTotleService.getsql(searchInfo, classification, condition, i, list1);
        }
        sql += getTotleService.getTotleSql(searchInfo, classifications);
        List<WholeHospitalsInfo> list = sqlMapper.selectList(sql, WholeHospitalsInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalsInfo> getOneChannelList(SevenReportInfo searchInfo) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        String sql1 = " select a.id,a.one_channel from " + tableName + " a where a.arrival_time>='" + searchInfo.getStartTime() + "' and a.arrival_time<='" + searchInfo.getEndTime() + "' group by one_channel order by a.one_channel";
        List<SevenReportInfo> list1 = sqlMapper.selectList(sql1, SevenReportInfo.class);
        String sql = " ";
        String classifications = "";
        if ("age_range".equals(searchInfo.getDimensionSelection()) || "one_medium".equals(searchInfo.getDimensionSelection())
                || "field_tc".equals(searchInfo.getDimensionSelection()) || "one_channel".equals(searchInfo.getDimensionSelection())
                || "cities".equals(searchInfo.getDimensionSelection()) || "occupation".equals(searchInfo.getDimensionSelection())) {
            classifications = " and a." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        } else {
            classifications = " and c." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        }
        for (int i = 0; i < list1.size(); i++) {
            String classification = list1.get(i).getOneChannel();
            String condition = "";
            if (classification == "" || classification == null) {
                condition = "a.one_channel is null" + classifications;
            } else {
                condition = "a.one_channel='" + classification + "'" + classifications;
            }
            sql += getTotleService.getsql(searchInfo, classification, condition, i, list1);
        }
        sql += getTotleService.getTotleSql(searchInfo, classifications);
        List<WholeHospitalsInfo> list = sqlMapper.selectList(sql, WholeHospitalsInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalsInfo> getOccupation(SevenReportInfo searchInfo) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        String sql1 = " select a.id,a.occupation from " + tableName + " a where a.arrival_time>='" + searchInfo.getStartTime() + "' and a.arrival_time<='" + searchInfo.getEndTime() + "' group by occupation order by a.occupation";
        List<SevenReportInfo> list1 = sqlMapper.selectList(sql1, SevenReportInfo.class);
        String sql = " ";
        String classifications = "";
        if ("age_range".equals(searchInfo.getDimensionSelection()) || "one_medium".equals(searchInfo.getDimensionSelection())
                || "field_tc".equals(searchInfo.getDimensionSelection()) || "one_channel".equals(searchInfo.getDimensionSelection())
                || "cities".equals(searchInfo.getDimensionSelection()) || "occupation".equals(searchInfo.getDimensionSelection())) {
            classifications = " and a." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        } else {
            classifications = " and c." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        }
        for (int i = 0; i < list1.size(); i++) {
            String classification = list1.get(i).getOccupation();
            String condition = "";
            if (classification == "" || classification == null) {
                condition = "a.occupation is null" + classifications;
            } else {
                condition = "a.occupation='" + classification + "'" + classifications;
            }
            sql += getTotleService.getsql(searchInfo, classification, condition, i, list1);
        }
        sql += getTotleService.getTotleSql(searchInfo, classifications);
        List<WholeHospitalsInfo> list = sqlMapper.selectList(sql, WholeHospitalsInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalsInfo> getDepartment(SevenReportInfo searchInfo) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        String tableName1 = tableHead + "deal_info";
        String sql1 = " select a.id,c.department from " + tableName1 + " a  left join \n" +
                "" + tableName + " b on a.customer_id = b.id left join pay_service_info c\n" +
                "on a.pay_service_id=c.id where b.arrival_time>='" + searchInfo.getStartTime() + "' and b.arrival_time<='" + searchInfo.getEndTime() + "' group by c.department ";
        List<SevenReportInfo> list1 = sqlMapper.selectList(sql1, SevenReportInfo.class);
        String sql = " ";
        String classifications = "";
        if ("age_range".equals(searchInfo.getDimensionSelection()) || "one_medium".equals(searchInfo.getDimensionSelection())
                || "field_tc".equals(searchInfo.getDimensionSelection()) || "one_channel".equals(searchInfo.getDimensionSelection())
                || "cities".equals(searchInfo.getDimensionSelection()) || "occupation".equals(searchInfo.getDimensionSelection())) {
            classifications = " and a." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        } else {
            classifications = " and c." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        }
        for (int i = 0; i < list1.size(); i++) {
            String classification = list1.get(i).getDepartment();
            String condition = "";
            if (classification == "" || classification == null) {
                condition = "c.department is null" + classifications;
            } else {
                condition = "c.department='" + classification + "'" + classifications;
            }
            sql += getTotleService.getsql(searchInfo, classification, condition, i, list1);
        }
        sql += getTotleService.getTotleSql(searchInfo, classifications);
        List<WholeHospitalsInfo> list = sqlMapper.selectList(sql, WholeHospitalsInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalsInfo> getDoctorList(SevenReportInfo searchInfo) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "deal_info";
        String sql1 = " select a.id,a.doctor from " + tableName + " a where a.operation_time>='" + searchInfo.getStartTime() + "' and a.operation_time<='" + searchInfo.getEndTime() + "' group by doctor order by a.doctor";
        List<SevenReportInfo> list1 = sqlMapper.selectList(sql1, SevenReportInfo.class);
        String sql = "";
        String classifications = "";
        if ("age_range".equals(searchInfo.getDimensionSelection()) || "one_medium".equals(searchInfo.getDimensionSelection())
                || "field_tc".equals(searchInfo.getDimensionSelection()) || "one_channel".equals(searchInfo.getDimensionSelection())
                || "cities".equals(searchInfo.getDimensionSelection()) || "occupation".equals(searchInfo.getDimensionSelection())) {
            classifications = " and a." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        } else {
            classifications = " and c." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        }
        for (int i = 0; i < list1.size(); i++) {
            String classification = list1.get(i).getDoctor();
            String condition = "";
            if (classification == "" || classification == null) {
                condition = "b.doctor is null" + classifications;
            } else {
                condition = "b.doctor='" + classification + "'" + classifications;
            }
            sql += getTotleService.getsql(searchInfo, classification, condition, i, list1);
        }
        sql += getTotleService.getTotleSql(searchInfo, classifications);
        List<WholeHospitalsInfo> list = sqlMapper.selectList(sql, WholeHospitalsInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalsInfo> getPayServiceList(SevenReportInfo searchInfo) {
        String sql1 = "select a.id,a.doctor as doctor from operation_analysis_info a group by  a.doctor";
        List<WholeHospitalsInfo> list1 = sqlMapper.selectList(sql1, WholeHospitalsInfo.class);
        String sql = "";
        List<WholeHospitalsInfo> list = sqlMapper.selectList(sql, WholeHospitalsInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalsInfo> getCitiesList(SevenReportInfo searchInfo) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        String sql1 = " select a.id,a.cities from " + tableName + " a group by cities order by a.cities";
        List<SevenReportInfo> list1 = sqlMapper.selectList(sql1, SevenReportInfo.class);
        String sql = "";
        String classifications = "";
        if ("age_range".equals(searchInfo.getDimensionSelection()) || "one_medium".equals(searchInfo.getDimensionSelection())
                || "field_tc".equals(searchInfo.getDimensionSelection()) || "one_channel".equals(searchInfo.getDimensionSelection())
                || "cities".equals(searchInfo.getDimensionSelection()) || "occupation".equals(searchInfo.getDimensionSelection())) {
            classifications = " and a." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        } else {
            classifications = " and c." + searchInfo.getDimensionSelection() + "='" + searchInfo.getValue() + "'";
        }
        for (int i = 0; i < list1.size(); i++) {
            String classification = list1.get(i).getCities();
            String condition = "";
            if (classification == "" || classification == null) {
                condition = "a.cities is null" + classifications;
            } else {
                condition = "a.cities='" + classification + "'" + classifications;
            }
            sql += getTotleService.getsql(searchInfo, classification, condition, i, list1);
        }
        sql += getTotleService.getTotleSql(searchInfo, classifications);
        List<WholeHospitalsInfo> list = sqlMapper.selectList(sql, WholeHospitalsInfo.class);
        return list;
    }

    public String getTableName() {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return tableName;
    }

}
